<template>
  <div class="nav-menu">
    <div class="header">
      <img src="~assets/img/logo.svg" alt="" />
      <span v-if="!Collapse" class="title">VUE+TS</span>
    </div>
    <el-menu
      :default-active="defaultValue"
      class="el-menu-vertical-demo"
      background-color="#000"
      text-color="#fff"
      active-text-color="#ffd04b"
      :collapse="Collapse"
      unique-opened
    >
      <template v-for="item in userMenus" :key="item.id">
        <template v-if="item.type === 1">
          <el-submenu class="sub-title" :index="item.id + ''">
            <template #title>
              <i v-if="item.icon" :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item
                :index="subitem.id + ''"
                @click="headleMenuItemClick(subitem)"
              >
                <template #title>
                  <i v-if="subitem.icon" :class="subitem.icon"></i>
                  <span>{{ subitem.name }}</span>
                </template>
              </el-menu-item>
            </template>
          </el-submenu>
        </template>

        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <template #title>
              <i v-if="item.icon" :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { useStore } from '../../../store/store'
import { useRouter, useRoute } from 'vue-router'
import { pathMapToMenu } from '../../../utils/map-menus'
export default defineComponent({
  props: {
    Collapse: {
      type: Boolean,
      default: false
    }
  },

  setup(props, { emit }) {
    //store
    const store = useStore()
    const userMenus = computed(() => {
      return store.state.login.userMenus
    })
    //router
    const router = useRouter()
    const route = useRoute()
    const currentPath = route.path

    const menu = pathMapToMenu(userMenus.value, currentPath)
    const defaultValue = ref(menu.id + '')

    const headleMenuItemClick = (item: any) => {
      console.log(item)
      router.push({
        path: item.url ?? '/notFount'
      })
    }
    return {
      userMenus,
      headleMenuItemClick,
      defaultValue
    }
  }
})
</script>

<style scoped>
.header {
  display: flex;
  height: 40px;
  line-height: 40px;
}
img {
  margin-left: 10px;
  width: 30px;
}
.title {
  color: rgb(255, 255, 255);
  font-size: 14px;
  margin-left: 10px;
}
.sub-title {
  color: aliceblue;
}
.el-menu {
  border-right: none;
}
</style>
